
Imports System
Imports System.Data.SqlClient

Namespace _2bMatesSite.Common
	''' <summary>
	''' Generic DB utilities.
	''' </summary>
	Public Class DBManager
		' NOTE: the client is responsible
		' for closing it's connections in
		' the finally clause of the catch statement, preferrably
		Public Shared Function GetUnmanagedConnection() As SqlConnection
			Dim sqlConn As New SqlConnection()

			sqlConn.ConnectionString = Constants.CONN_STRING
			sqlConn.Open()

			Return sqlConn
		End Function

		Public Shared Function GetUnmanagedForumDBConnection() As SqlConnection
			Dim sqlConn As New SqlConnection()

			sqlConn.ConnectionString = Constants.FORUM_DB_CONN_STRING
			sqlConn.Open()

			Return sqlConn
		End Function

		Public Shared Function SetNZValue(ByVal cmd As SqlCommand, ByVal field As String, ByVal value As String) As SqlParameter
			Dim par As SqlParameter = Nothing
			If value <> Nothing AndAlso value.Length > 0 Then
				par = cmd.Parameters.Add(field, value)
			Else
				par = cmd.Parameters.Add(field, DBNull.Value)
			End If
			Return par
		End Function

		Public Shared Function SetNZValue(ByVal cmd As SqlCommand, ByVal field As String, ByVal value As Integer) As SqlParameter
			Dim par As SqlParameter = Nothing
			If value >= 0 Then
				par = cmd.Parameters.Add(field, value)
			Else
				par = cmd.Parameters.Add(field, DBNull.Value)
			End If
			Return par
		End Function

		Public Shared Function SetNZValue(ByVal cmd As SqlCommand, ByVal field As String, ByVal value As DateTime) As SqlParameter
			Dim par As SqlParameter = Nothing
			If value <> DateTime.MinValue Then
				par = cmd.Parameters.Add(field, value)
			Else
				par = cmd.Parameters.Add(field, DBNull.Value)
			End If
			Return par
		End Function

		Public Shared Function GetNZString(ByVal dr As SqlDataReader, ByVal fieldName As String) As String
			Return GetNZString(dr, fieldName, Nothing)
		End Function

		Public Shared Function GetNZString(ByVal dr As SqlDataReader, ByVal fieldName As String, ByVal defaultValue As String) As String
			Dim ord As Integer = dr.GetOrdinal(fieldName)
			If dr.IsDBNull(ord) Then
				Return defaultValue
			Else
				Return dr.GetString(ord)
			End If
		End Function

		Public Shared Function GetNZInt32(ByVal dr As SqlDataReader, ByVal fieldName As String) As Integer
			Return GetNZInt32(dr, fieldName, -1)
		End Function

		Public Shared Function GetNZInt32(ByVal dr As SqlDataReader, ByVal fieldName As String, ByVal defaultValue As Integer) As Integer
			Dim ord As Integer = dr.GetOrdinal(fieldName)
			If dr.IsDBNull(ord) Then
				Return defaultValue
			Else
				Return dr.GetInt32(ord)
			End If
		End Function

		Public Shared Function GetNZBool(ByVal dr As SqlDataReader, ByVal fieldName As String) As Boolean
			Return GetNZBool(dr, fieldName, False)
		End Function

		Public Shared Function GetNZBool(ByVal dr As SqlDataReader, ByVal fieldName As String, ByVal defaultValue As Boolean) As Boolean
			Dim ord As Integer = dr.GetOrdinal(fieldName)
			If dr.IsDBNull(ord) Then
				Return defaultValue
			Else
				Return dr.GetBoolean(ord)
			End If
		End Function

		Public Shared Function GetNZDateTime(ByVal dr As SqlDataReader, ByVal fieldName As String) As DateTime
			Return GetNZDateTime(dr, fieldName, DateTime.MinValue)
		End Function

		Public Shared Function GetNZDateTime(ByVal dr As SqlDataReader, ByVal fieldName As String, ByVal defaultValue As DateTime) As DateTime
			Dim ord As Integer = dr.GetOrdinal(fieldName)
			If dr.IsDBNull(ord) Then
				Return defaultValue
			Else
				Return dr.GetDateTime(ord)
			End If
		End Function
	End Class
End Namespace






